﻿using RecruitmentPage.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RecruitmentPage.Controllers
{
    public class EmployerController : Controller
    {

        public ActionResult Employer()
        {
            return View();
        }


        public ActionResult DataAdmin()
        {
            return View();
        }

        public ActionResult WorkerDetail(string personid)
        {
            ViewBag.ID = personid;
            return View();
        }

        [HttpPost]
        public ActionResult Upload(string personid)
        {
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Files/"), fileName);
                    file.SaveAs(path);
                    byte[] bytes = System.IO.File.ReadAllBytes(path);
                    //string result = System.Text.Encoding.ASCII.GetString(bytes);


                    FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                    BinaryWriter br = new BinaryWriter(fs);
                    //Byte[] bytData = new byte[fs.Length];
                    br.Write(bytes);
                    fs.Dispose();

                    Guid id = Guid.Parse(personid);

                    RPContext db = new RPContext();
                    var PersonalInfor = (from p in db.PersonalInfor where p.UniqueId == id select p).SingleOrDefault();
                    PersonalInfor.CVName = fileName;
                    PersonalInfor.CV = bytes;
                    db.SaveChanges();
                }
            }

            return RedirectToAction("Employer");
        }

        [HttpPost]
        public string DownloadCV(string id)
        {
            RPContext db = new RPContext();
            Guid personid = Guid.Parse(id);
            var PersonalInfor = (from p in db.PersonalInfor where p.UniqueId == personid select p).SingleOrDefault();
            //C:\Users\haovt_000.haovt2408\Documents\visual studio 2013\Projects\RecruitmentPage\Files
            string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            string pathDownload = Path.Combine(pathUser, "Downloads");
            pathDownload += "\\" +PersonalInfor.CVName;

            byte[] blob = PersonalInfor.CV;
            FileStream fs = new FileStream(pathDownload, FileMode.CreateNew, FileAccess.Write, FileShare.None);
            
            fs.Write(blob, 0, blob.Length);
            fs.Flush();
            fs.Close();


            //MemoryStream ms = new MemoryStream();
            //ms.Write(PersonalInfor.CV, 0, PersonalInfor.CV.Length);
            //ms.Close();
            //byte[] bytesInStream = ms.ToArray(); // simpler way of converting to array
            //ms.Close(); 

            //Response.Clear();
            //Response.ContentType = "application/force-download";
            //Response.AddHeader("content-disposition", "attachment;    filename=nabc.doc");
            //Response.BinaryWrite(bytesInStream);
            //Response.End();

            //String FilePath = "~/Files/Test.doc";
            //String FileName = "Testhere";

            //System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            //response.ClearContent();
            //response.Clear();
            //response.ContentType = "text/plain";
            //response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
            //response.TransmitFile(FilePath);
            //response.Flush();
            //response.End();

            return "The download has completed in path: " + pathDownload;
        }

	}
}